#include "config.h"

#include <stdio.h>
#include <math.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif

/*------------------------------------------------------------------
 * Gattrib specific includes
 *------------------------------------------------------------------*/
#include <libgeda/libgeda.h>       /* geda library fcns  */
#include "struct.h"     /* typdef and struct declarations */
#include "prototype.h"  /* function prototypes */
#include "globals.h"

#ifdef HAVE_LIBDMALLOC
#include <dmalloc.h>
#endif


/*------------------------------------------------------------------
 * The below fcns identical to those defined in
 * geda-gnetlist/src/s_misc.c
 *------------------------------------------------------------------*/
/*!
 * Running count of number of characters printed on current line.
 */
static int char_index = 0;

/*! \brief Print message in verbose mode
 *
 * Print the supplied message in verbose mode. Line wrap if necessary.
 *
 * Identical to that defined in gnetlist/src/s_misc.c
 * \param string String to be printed
 */
void verbose_print(char *string)
{
    if (verbose_mode) {
	printf("%s", string);
	char_index++;
	if ((char_index + 1) >= 78) {
	    printf("\n");
	    char_index = 0;
	}
    }
}

/*! \brief Print "DONE" message in verbose mode
 *
 * Prints the "DONE" message in verbose mode, wrapping before printing
 * if near the end of line.
 *
 * Identical to function defined in gnetlist/src/s_misc.c
 */
void verbose_done(void)
{
    if (verbose_mode) {
	if (char_index >= 70) {
	    printf("\nDONE\n");
	} else {
	    printf(" DONE\n");
	}

	char_index = 0;
    }
}

/*! \brief Reset the running character count
 *
 * Reset the current characted count.
 *
 * Identical to function defined in gnetlist/src/s_misc.c
 */

void verbose_reset_index(void)
{
    char_index = 0;
}


/*------------------------------------------------------------------
 * Gattrib specific utilities
 *------------------------------------------------------------------*/
char *s_misc_remaining_string(gchar *string, gchar delimiter, gint count)
{
  gint i;
  gchar *remaining;
  gchar *return_value;

  /* find count'th delimiter */
  remaining = string;
  for (i = 0; i < count; i++) {
    remaining = strchr(remaining, delimiter);
    if (!remaining) {
      return (NULL);
    }
    remaining++;
  }

  /* skip whitespace */
  while (*remaining == ' ') {
    remaining++;
  }
  if (!(*remaining)) {
    return (NULL);
  }

  /* copy remainder into allocated return string */
  return_value = g_strdup(remaining);

  /* return string */
  return (return_value);
}
